#!/bin/bash
#v0.7 增加ip地址提示，改写函数方式
#v1.00 支持版本更新
#v1.01 增加netty心跳检测
#v1.20 修改端口检测机制
#v2.00 隐藏部分日志显示，全部日志写入日志文件中.
#v2.00 添加更改CMDB状态, 1,为发布成功; 2, 为发布中; 3, 为发布失败

export LANG="en_US.UTF-8"
source /etc/profile
VerSion=1.00
VersionServerIP=10.201.32.109
VersionType=netty

if [ "$3" = "" ];then
    logfile=/home/netty/$2_monitor_`date +%Y%m%d%H%M%S`_spring.log
else
    logfile=$3
fi

#日志记录在指定的文件中
Setlog(){
    echo $1 >> $logfile
}

getPortConfig(){
  args=($@)
  echo `sed -n "/${arg1}=/"p ${portConfig} | sed "s/${arg1}=//"`
}


baseDir="/opt/app/spring"
portConfig="/opt/app/appPorts.properties"
arg1=${2}
export portStart="`getPortConfig`"

function GetIP()
{
LocalIP=`/sbin/ifconfig |grep "inet addr" |awk '{print $2}' |awk -F : '{print $2}' | grep -v '127.0.0.1'`
}

function SetStatus(){
    sid=$1
    GetIP
    curl -s -m 2 http://cmdb.bl.com/blapp/srelease/?sid=$sid\&appip=$LocalIP\&appport=$portStart\&operation=fabu-sit
    if [ $? -eq 0 ];then
        case $sid in
            1)
                echo "cmdb状态已更新为: 发布成功"
                Setlog "cmdb状态已更新为: 发布成功"
            ;;
            2)
                echo "cmdb状态已更新为: 发布中"
                Setlog "cmdb状态已更新为: 发布中"
            ;;

            3)
                echo "cmdb状态已更新为: 发布失败"
                Setlog "cmdb状态已更新为: 发布失败"
            ;;
            *)
                echo "参数传入错误,请检查!!!"
                Setlog "参数传入错误,请检查!!!"
            esac
    else
        echo "cmdb 修改失败,请检查!!!"
        Setlog "cmdb 修改失败,请检查!!!"
    fi
}



function PortsCheck()
{
echo "---->2.1 获取端口号..."
Setlog "---->2.1 获取端口号..."
echo "    |--->[√]启动端口号是:$portStart"
Setlog "    |--->[√]启动端口号是:$portStart"
echo "    |--->[√]关闭端口号是:$portStop"
Setlog "    |--->[√]关闭端口号是:$portStop"
if      
    [ ! $portStart  ]
      then     
        echo "    |--->[×]发布脚本获取${2}端口失败,请检查传参！本次发布被取消！无需回滚"
        Setlog "    |--->[×]发布脚本获取${2}端口失败,请检查传参！本次发布被取消！无需回滚"
        exit 1
fi
echo ""
}

function PortStatus()
{
sleep 10
nc -v -w 10 -z 127.0.0.1 $portStart > /dev/null
if
	[ $? != 0 ]
        then
            echo "    |--->[×]$2服务异常"
            Setlog "    |--->[×]$2服务异常"
            SetStatus 3
	    else
            Setlog "    |--->[√]$2服务启动正常"
            echo "    |--->[√]$2服务启动正常"
            SetStatus 1
fi
echo ""
}

function CheckHearbt()
{
#/usr/bin/curl  -H "Content-Type: application/json" -X POST   http://10.201.128.104:6080/app/heartBeat
ContentPath=`/usr/bin/curl -s -m 2 http://$VersionServerIP/$VersionType/content-path/content-path.txt | grep ${2}= | cut -d  = -f2`
#echo $ContentPath
HearbtStatus=`/usr/bin/curl -s -m 2 -H "Content-Type: application/json" -X POST   http://$LocalIP:$portStart/$ContentPath/heartBeat`
#echo $HearbtStatus
echo $HearbtStatus  | grep active  > /dev/null
if [ $? = 0 ]
then
	Setlog "    |--->[√]检测到$2心跳,状态如下:"
	Setlog "    ----------------------------------------------------------------------------------------------------------------"
	Setlog "    $HearbtStatus"
	Setlog "    ----------------------------------------------------------------------------------------------------------------"
else
	echo "    |--->[×]未检测到$2心跳，请检测进程或者上下文是否正确"
	Setlog  "    |--->[×]未检测到$2心跳，请检测进程或者上下文是否正确"
fi
}


Setlog "======================>【2.开始检测${2}服务状态】<========================="
echo "======================>【2.开始检测${2}服务状态】<========================="
sleep 1
PortsCheck $1 $2
Setlog "---->2.2 正在检测${2}:$portStart端口状态"
sleep 10
PortStatus $1 $2

Setlog "---->2.3 检测$2心跳状态"
GetIP 
#echo $LocalIP
CheckHearbt $1 $2


